package com.canbot.u05.activity.guide;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SoundManager;
import com.canbot.u05.utils.StatusBarUtil;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.GuideRecordDialog;

import java.io.File;
import java.util.HashMap;

/**
 * Created by zmp on 2019/4/10 17:08
 *
 * @author zmp
 * <p>
 * 导览语音选择界面
 */
public class GuideVoiceSetActivity extends BaseActivityNoTitle implements View.OnClickListener {


        private int type;

        private String audioPath;

        private String content;

        private GuideRecordDialog guideRecordDialog;

        private ToastTools toastTools;

        private View ivOk;

        private TextView tvLoad;

        private View ivPb;

        private CheckBox voiceCheckBox;

        private CheckBox ttsCheckBox;

        private CheckBox recordCheckBox;


        private static final int CHOOSE_AUDIO_CODE = 1001;

        private static final int CHOOSE_TTS_CODE = 1002;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_guide_voice_set);
                Intent intentResult = getIntent();
                type = intentResult.getIntExtra("type", -1);
                audioPath = intentResult.getStringExtra("path");
                content = intentResult.getStringExtra("content");
                toastTools = ToastTools.newInstance(this);
                initView();
        }

        private void initView() {
                findViewById(R.id.guide_voice_save).setOnClickListener(this);
                findViewById(R.id.guide_voice_back).setOnClickListener(this);
                findViewById(R.id.voiceItemFL).setOnClickListener(this);
                findViewById(R.id.ttsItemFL).setOnClickListener(this);
                findViewById(R.id.recordItemFL).setOnClickListener(this);
                voiceCheckBox = (CheckBox) findViewById(R.id.voiceCheckBox);
                ttsCheckBox = (CheckBox) findViewById(R.id.ttsCheckBox);
                recordCheckBox = (CheckBox) findViewById(R.id.recordCheckBox);
                setItemSelected();
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (data == null || resultCode != RESULT_OK) {
                        return;
                }
                //音频
                if (requestCode == CHOOSE_AUDIO_CODE) {
                        type = data.getIntExtra("type", -1);
                        audioPath = data.getStringExtra("path");
                        content = data.getStringExtra("content");
                }
                //文字
                if (requestCode == CHOOSE_TTS_CODE) {
                        type = data.getIntExtra("type", -1);
                        content = data.getStringExtra("content");
                        audioPath = content;
                        if (TextUtils.isEmpty(content)) {
                                type = -1;
                        }
                }
                setItemSelected();
        }

        private void setItemSelected() {
                voiceCheckBox.setChecked(false);
                ttsCheckBox.setChecked(false);
                recordCheckBox.setChecked(false);
                switch (type) {
                        case AudioEvent.AUDIO_TYPE_AUDIO:
                                voiceCheckBox.setChecked(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_TTS:
                                ttsCheckBox.setChecked(true);
                                break;
                        case AudioEvent.AUDIO_TYPE_RECODE:
                                recordCheckBox.setChecked(true);
                                break;
                        default:
                                break;
                }
        }

        @Override
        public void onClick(View v) {
                switch (v.getId()) {
                        case R.id.guide_voice_save:
                                if (type == -1 || TextUtils.isEmpty(content)) {
                                        toastTools.showText("请先添加语音!");
                                        return;
                                }

                                if (type == AudioEvent.AUDIO_TYPE_RECODE) {
                                        showLoadDialog();
                                        sendMsg2Chest(new AudioEvent(type, audioPath, content));
                                        return;
                                }
                                setVoiceResult();
                                break;
                        case R.id.guide_voice_back:
                                setResult(RESULT_CANCELED);
                                finish();
                                break;
                        case R.id.voiceItemFL:
                                Intent intent = new Intent(this, GuideChooseAudioActivity.class)
                                        .putExtra("type", type)
                                        .putExtra("content", content)
                                        .putExtra("path", audioPath);
                                startForResultActivity(intent, CHOOSE_AUDIO_CODE);
                                break;
                        case R.id.ttsItemFL:
                                Intent ttsIntent = new Intent(this, GuideChooseTtsActivity.class)
                                        .putExtra("type", type)
                                        .putExtra("content", content)
                                        .putExtra("path", audioPath);
                                startForResultActivity(ttsIntent, CHOOSE_TTS_CODE);
                                break;
                        case R.id.recordItemFL:
                                record();
                                break;
                        default:
                }
        }


        private void setVoiceResult() {
                Intent intent = new Intent();
                intent.putExtra("type", type);
                intent.putExtra("path", audioPath);
                intent.putExtra("content", content);
                setResult(RESULT_OK, intent);
                finish();
        }


        /**
         * 进入录音
         */
        private void record() {
                if (guideRecordDialog == null) {
                        //点击语音录入图标
                        guideRecordDialog = new GuideRecordDialog(this, new GuideRecordDialog.IRecodeListener() {
                                @Override
                                public void finish(String fileName) {
                                        Logger.e("test", "fileName" + fileName);
                                        if (TextUtils.isEmpty(fileName)) {
                                                toastTools.showText("请录入语音!");
                                                return;
                                        }
                                        Logger.d("SpeechSetupActivity", "录音的文件名：" + fileName);
                                        SoundManager.getInstance().playSound(PathConst.ROOT_PATH + PathConst.RECORD_FILE_PATH + fileName, null);
                                        type = AudioEvent.AUDIO_TYPE_RECODE;
                                        audioPath = PathConst.RECORD_FILE_PATH + fileName;
                                        content = fileName;
                                        setItemSelected();
                                }

                                @Override
                                public void cancel() {

                                }

                                @Override
                                public void error() {
                                        toastTools.showText("录音失败,请检查权限!");
                                }
                        });
                }
                guideRecordDialog.show();

        }

        private void sendMsg2Chest(final AudioEvent data) {
                String url = IpMsgConst.HTTP_URL + "guideMapAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 3 + "");
                hashMap.put("data", JSON.toJSONString(data));
                hashMap.put(data.getMusicName(), new File(PathConst.ROOT_PATH + data.getMusicPath()));
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {
                                Log.e("TTS2Voice", result);
                                loadDialog.dismiss();
                                toastTools.showText("上传成功!");
                                setVoiceResult();
                        }

                        @Override
                        public void onFailure() {
                                toastTools.showText("服务器异常！,请重试");
                                loadDialog.dismiss();
                                MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(8));
                        }
                });
        }

        private Dialog loadDialog;

        private void showLoadDialog() {
                if (loadDialog == null) {
                        loadDialog = new Dialog(this, R.style.MyDialogStyle);
                        loadDialog.setContentView(R.layout.guide_dialog_load);
                        ivOk = loadDialog.findViewById(R.id.guide_load_iv);
                        ivPb = loadDialog.findViewById(R.id.guide_load_pb);
                        tvLoad = (TextView) loadDialog.findViewById(R.id.guide_load_tv);
                }
                ivPb.setVisibility(View.VISIBLE);
                ivOk.setVisibility(View.INVISIBLE);
                tvLoad.setText("正在上传...");
                loadDialog.setCancelable(false);
                StatusBarUtil.hideVirtualKey(loadDialog);
                loadDialog.show();
        }

}
